package com.zhaiweijia.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.zhaiweijia.entity.Article;
import com.zhaiweijia.entity.Category;
import com.zhaiweijia.entity.Channel;
import com.zhaiweijia.entity.Slide;
import com.zhaiweijia.service.ArticleService;
import com.zhaiweijia.service.ChannelService;
import com.zhaiweijia.service.SlideService;
import com.zhaiweijia.util.DateUtil;

@Controller

public class IndexController {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private SlideService slideService;
	@Autowired
	private ChannelService channelService;
	
	/**
	 * 文章详情
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("detail")
	public String detail(Integer articleId,Model model) {
		
		Article article	=articleService.findDetailById(articleId);
			
		model.addAttribute("article", article);
		return "index/detail";
		
	}
	/**
	 * 首页
	 * @param article
	 * @param pageNum
	 * @param pageSize
	 * @param model
	 * @return
	 */
	//首页
	@RequestMapping({"/","","index"})
	public String index(Article article,@RequestParam(defaultValue = "1")Integer pageNum,
			@RequestParam(defaultValue = "3")Integer pageSize,Model model) {
		//获取所有的栏目数据
		List<Channel> channels = channelService.getChannels();
		model.addAttribute("channels", channels);
		
		//判断如果选中栏目 则查询栏目数据否则显示热点数据
		if(article.getChannelId() ==null ) {
			//热点
			article.setHot(1);
		}else {
			//根据栏目频道获取旗下的所有分类
			List<Category> categories = channelService.getCategoryIdByChannelId(article.getChannelId());			
			model.addAttribute("categories", categories);
		}
	
		//审核通过
		article.setStatus(1);
	
		//获取数据
		PageInfo info = articleService.list(pageNum, pageSize, article);
		//轮播图
		List<Slide> slides=slideService.getSlides();
		model.addAttribute("slides", slides);
		
		/* 右边侧边栏 */
		/* 24小时热门 */
		//设置时间
		Article a24 = new Article();
		a24.setCreated(DateUtil.offsetDate(-1));
		a24.setStatus(1);//审核通过
		
		PageInfo info24Hot = articleService.list(1, 5, a24);
		model.addAttribute("info24Hot", info24Hot);
		//将数据传入到页面
		model.addAttribute("info", info);
		model.addAttribute("a24", a24);
		//点击量排行榜
		Article aHit = new Article();
		aHit.setStatus(1);//审核通过
		
		PageInfo infoHits = articleService.listByHits(1, 5, aHit);
		//将数据传入到页面
		model.addAttribute("infoHits", infoHits);
		
		
		return "index/index";
	}
	
	
}
